package com.example.medicine.View;

import com.example.medicine.Bean.zhongyao;
import com.example.medicine.Bean.zhongyao_zhengzhuang;
import com.example.medicine.Mapper.yonghuMapper;
import com.example.medicine.Mapper.zhongyaoMapper;
import com.example.medicine.Mapper.zhongyao_zhengzhuangMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@Slf4j
@Controller
public class ZhongyaoView {
    @Autowired
    private zhongyaoMapper zhongyaoMapper;
    @Autowired
    private yonghuMapper yonghuMapper;
    @Autowired
    private zhongyao_zhengzhuangMapper zhongyaoZhengzhuangMapper;
    @RequiresPermissions("中药管理")
    @RequestMapping("/zyxx")
    public String zhongyaoShow(Model model){
        log.info("中药列表页面");
        List<String> yonghuId = yonghuMapper.getUserIdFour();
        List<String> yonghuCompany = yonghuMapper.getCompany();
        model.addAttribute("yonghuId",yonghuId);
        model.addAttribute("yonghuCompany",yonghuCompany);
        return "pages/zyxx/list";
    }
    @RequiresPermissions("中药管理")
    @RequestMapping("/zyxx/create")
    public String zhongyaoCreateShow(){
        log.info("中药创建页面");
        return "pages/zyxx/create";
    }
    @RequiresPermissions("中药管理")
    @RequestMapping("/zyxx/edit/{id}")
    public String getzhongyaoById(@PathVariable long id, Model model) {
        log.info("中药编辑页面");
        zhongyao zhongyao=zhongyaoMapper.getById(id);
        zhongyao.setId(id);
        List<zhongyao_zhengzhuang> zhongyao_zhengzhuangList= zhongyaoZhengzhuangMapper.getByZhongyao((int) id);
        StringBuilder zhengzhuang= new StringBuilder();
        for(zhongyao_zhengzhuang x:zhongyao_zhengzhuangList ){
            String more=",";
            String temp=x.getZhengzhuang();
            if(temp.charAt(temp.length()-1)==',')
                more = "";
            zhengzhuang.append(temp).append(more);
        }
        String[] elements = zhengzhuang.toString().split(",");
        Set<String> uniqueElementsSet = new LinkedHashSet<>(Arrays.asList(elements));
        String outputString = String.join(",", uniqueElementsSet)+",";
        model.addAttribute("zhongyao",zhongyao);
        model.addAttribute("zhengzhuang", outputString.equals(",") ?"":outputString);
        return "pages/zyxx/edit";
    }
    @RequiresPermissions("中药管理")
    @RequestMapping("/zyxx/batchAdd")
    public String batchAddShow(){
        log.info("中药批量导入页面");
        return "pages/zyxx/batchAdd";
    }
}